// pages/consult/clickConsult/clickConsult.js

Page({
  /**
   * 页面的初始数据
   */
  data: {
    form: {
      //咨询师名字
      name: "",
      //地址
      address: "",
      detailValue: [],//选中的标签数据
      level: "",//擅长
      //选中的咨询师性别
      sex: '',
    },

    //三级联动数据
    region: [],
    //标签数据
    riderCommentList: [{
      value: '',
      selected: false,
      title: '全部'
    }, {
      value: '婚姻家庭',
      selected: false,
      title: '婚姻家庭'
    }, {
      value: '抑郁症',
      selected: false,
      title: '抑郁症'
    }, {
      value: '焦虑障碍',
      selected: false,
      title: '焦虑障碍'
    }, {
      value: '职场压力',
      selected: false,
      title: '职场压力'
    }, {
      value: '青少年心理',
      selected: false,
      title: '青少年心理'
    }],
    //咨询师等级
    city2Text: '',
    city2Value: [],
    city2Title: '',
    citys: [
      { label: '高级咨询师', value: '高级咨询师' },
      { label: '中级咨询师', value: '中级咨询师' },
      { label: '初级咨询师', value: '初级咨询师' },
    ],
    //咨询师性别数组
    buttons: [{ id: 1, name: '男', checked: true }, { id: 2, name: '女', checked: false }, { id: 3, name: '不限', checked: false }],
    //咨询方式
    buttonWay: [{ id: 1, name: "全部方式", checked: true }, { id: 2, name: "视频咨询", checked: false }, { id: 3, name: "语音咨询", checked: false }],
  },
  way: "",

  //输入框
  userNameInputAction: function (options) {
    let value = options.detail.value;
    this.setData({
      form: {
        ...this.data.form,
        name: value
      },
    });
  },

  //地址函数
  //点击确定按钮
  bindRegionChange(e) {
    const that = e.detail.value;
    this.setData({
      region: e.detail.value,
      form: {
        ...this.data.form,
        address: that[1],
      },
    });
    // console.log(this.data.region);
    console.log(this.data.form.address);
  },

  //最多只能选择三个标签
  checkboxChange(e) {
    console.log('checkboxChange e:', e);
    let string = "riderCommentList[" + e.target.dataset.index + "].selected"
    console.log(string);
    this.setData({
      ...this.data.form,
      [string]: !this.data.riderCommentList[e.target.dataset.index].selected
    });
    if (this.data.form.detailValue.indexOf(this.data.riderCommentList[e.target.dataset.index].value) !== -1) {
      var index = this.data.form.detailValue.findIndex(value => value == this.data.riderCommentList[e.target.dataset.index].value);
      this.data.form.detailValue.splice(index, 1);
      console.log(this.data.form.detailValue);

    };
    if (this.data.form.detailValue.length < 3) {
      this.data.form.detailValue = this.data.riderCommentList.filter(it => it.selected).map(it => it.value);
    } else {
      this.setData({
        ...this.data.form,
        [string]: !this.data.riderCommentList[e.target.dataset.index].selected,
      })
      wx.showToast({
        title: '最多只能选择三个',
        icon: 'none',
        duration: 1000,
      });
    };
    console.log('所有选中的值为：', this.data.form.detailValue);
  },
  //下拉框
  onColumnChange(e) {
    console.log('picker pick:', e);
  },
  onPickerChange(e) {
    const { key } = e.currentTarget.dataset;
    const { value } = e.detail;
    console.log('picker change:确认', e.detail);
    this.setData({
      [`${key}Visible`]: false,
      [`${key}Value`]: value,
      [`${key}Text`]: value.join(' '),
      form: {
        ...this.data.form,
        level: value,
      },
    });
    console.log(this.data.form.level);
  },
  onPickerCancel(e) {
    const { key } = e.currentTarget.dataset;
    console.log(e, '取消');
    console.log('picker1 cancel:');
    this.setData({
      [`${key}Visible`]: false,
    });
  },
  onWithoutTitlePicker() {
    this.setData({ city2Visible: true, city2Title: '' });
  },

  //  性别单选
  radioButtonTap: function (e) {
    let id = e.currentTarget.dataset.id;
    let sex = e._relatedInfo.anchorTargetText;
    for (let i = 0; i < this.data.buttons.length; i++) {
      if (this.data.buttons[i].id == id) {
        //当前点击的位置为true即选中
        this.data.buttons[i].checked = true;
      }
      else {
        //其他的位置为false
        this.data.buttons[i].checked = false;
      }
    }
    this.setData({
      buttons: this.data.buttons,
      form: {
        ...this.data.form,
        sex: sex,
      },
    });
    console.log(this.data.form.sex);

  },
  //咨询方式单选
  radioButtonTapWay: function (e) {
    let id = e.currentTarget.dataset.id;
    let way = e._relatedInfo.anchorTargetText;

    for (let i = 0; i < this.data.buttonWay.length; i++) {
      if (this.data.buttonWay[i].id == id) {
        //当前点击的位置为true即选中
        this.data.buttonWay[i].checked = true;
      }
      else {
        //其他的位置为false
        this.data.buttonWay[i].checked = false;
      }
    }
    this.setData({
      buttonWay: this.data.buttonWay,
      way: way,
    })
    console.log(this.data.way);
  },
  // 底部按钮
  // 提交按钮
  formSubmit: function (e) {  
    const objectString = JSON.stringify(this.data.form);
    wx.navigateTo({
      url: `/pages/consult/clickConsult/clickConsultQry/clickConsultQry?objectData=${encodeURIComponent(objectString)}`,
    });
    wx.showToast({
      title: '提交成功',
      icon: 'success',
    });
  },
  formReset: function (e) {
    console.log('重置');
    wx.reLaunch({
      url: "/pages/consult/clickConsult/clickConsult",
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    this.data.buttons[0].checked = true;
    this.setData({
      buttons: this.data.buttons,
    })
    console.log(this.data.buttons);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})